Beginning+Python 2.compressed by Unknown

Beginning+Python 2.compressed by Unknown

Author:Unknown
Language: eng
Format: mobi, epub
Published: 2015-09-11T18:57:58.789643+00:00


Chapter 18: Numerical Programming

Lists or Tuples?

Which should you use for arrays: lists or tuples? Remember that lists can be

modified, whereas tuples cannot. Therefore, if you need to add to, remove from,

or change the array, use a list. Though you can perform these operations on a

tuple by creating a new tuple with numbers added, removed, or changed, this is

more difficult to code and often runs more slowly. For fixed sequences of

numbers, you can use tuples.

Let ? s take another example of a function that operates on an array. You already wrote a function that computes the mean of an array of numbers. Now write a function that computes the standard

deviation. To remind you, the standard deviation is an indication of how much the numbers vary among themselves. If they ? re all almost the same, the standard deviation will be small, whereas if they are all over the place, the standard deviation will be large. The formula for the standard deviation that you will use is shown in Figure 18 - 1.

Figure 18-1

Here x , . . . , x are the numbers in the array, ยต is their mean, and N is the length of the array.

1

N

You could implement a standard deviation function several different ways. Here ? s one of them: from math import sqrt

def stddev(numbers):

n = len(numbers)

sum = 0

sum_of_squares = 0

for number in numbers:

sum += number

sum_of_squares += number * number

return sqrt(sum_of_squares / n - (sum / n) ** 2)

This function loops over the numbers to compute their sum of squares. Simultaneously, it computes their sum, because it needs that to compute the mean. The last line computes the standard deviation according to the preceding formula. You might have noticed that the function uses number * number when computing the sum of squares instead of number ** 2 ; that ? s because squaring a number by

multiplying it by itself is faster than using the general exponentiation operator.

Watch stddev in action. Remember that it takes one argument, a sequence of numbers (not several numerical arguments):

>

>

>

print(stddev((5.6, 3.2, -1.0, 0.7)))

2.50137462208

381

Part III: Putting Python to Work

Think for a moment about some advantages and drawbacks of using lists of numbers for arrays:

โ‘

The elements of a Python list need not be of the same type. You can create a list for which some elements are int , float , long , and double , or other objects like strings or even other sequences. For some applications, this is very handy. For instance, you may want to store None in a sequence to indicate that a value is not known. For other applications, it ? s important to make sure that all of the values in an array are of the same type. In that case, you ? ll have to write extra code to ensure this.

โ‘

Lists are single - dimensional, which makes them natural for expressing one - dimensional arrays.

You can create two - dimensional arrays as lists of lists and higher - dimensional arrays

analogously, but this can get complicated.

โ‘

Lists are a standard part of Python.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55911)
What's Done in Darkness by Kayla Perrin(26526)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19009)
The Fifty Shades Trilogy & Grey by E L James(18961)
Shot Through the Heart by Mercy Celeste(18881)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16986)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16878)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16806)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16702)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16329)
The Subtle Art of Not Giving a F*ck by Mark Manson(14262)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13424)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10924)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9478)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9317)
This is Going to Hurt by Adam Kay(9105)